<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{asset('lib/layui-v2.6.3/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/public.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/app.css')}}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">客户名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">客户级别</label>
                                <div class="layui-input-inline">
                                    <select name="agent_level_id">
                                        <option value=""></option>
                                        <option value="1">一级代理</option>
                                        <option value="2">二级代理</option>
                                    </select>
                                </div>
                            </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="realname" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="button" class="layui-btn layui-btn-primary" onclick="complexSearch()"><i class="layui-icon">&#xe628;</i> 复杂查询</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-recharge" lay-event="recharge">充值</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<!--复杂查询弹出层内容开始-->
<div id="popSearch" style="display: none">
    <form class="layui-form layui-form-pane" id="popSearchForm" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">客户级别</label>
                <div class="layui-input-inline">
                    <select name="agent_level_id">
                        <option value=""></option>
                        <option value="1">一级代理</option>
                        <option value="2">二级代理</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="realname" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">登录账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">手机号码</label>
                <div class="layui-input-inline">
                    <input type="text" name="mob" autocomplete="off" class="layui-input" lay-verify="number">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">余额范围</label>
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="balance_min" placeholder="￥" autocomplete="off" class="layui-input" lay-verify="number">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="balance_max" placeholder="￥" autocomplete="off" class="layui-input" lay-verify="number">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">注册时间</label>
                    <div class="layui-input-inline">
                        <input style="width: 400px" type="text" name="range_date" id="range_date" class="layui-input" placeholder="点击选择日期">
                    </div>
            </div>

            <div class="layui" style="text-align: center">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn-high"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </div>
    </form>
</div>
<!--复杂查询弹出层内容结束-->
<script src="{{asset('lib/layui-v2.6.3/layui.js')}}" charset="utf-8"></script>
<script src="{{asset('lib/jquery-3.4.1/jquery-3.4.1.min.js')}}" charset="utf-8"></script>
<script>
    //复杂查询弹出层
    function complexSearch() {
        layer.open({
            type:1,
            title: "复杂查询",
            area: ['50%'],
            content: $('#popSearch'),
            cancel:function () {
                $('#popSearchForm')[0].reset();
                $('#popSearch').hide();
            }
        });

    }
        layui.use(['form', 'table', 'laydate'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;
            //日期控件
            laydate.render({
                elem: '#range_date',
                type: 'datetime',
                range: '~'
            });

        table.render({
            elem: '#currentTableId',
            url: '/api/agent/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: "2%"},
                {field: 'id', width: "7%", title: 'ID', sort: true},
                {field: 'name', width: "18%", title: '客户名称'},
                {field: 'agent_level_id', width: "10%", title: '代理级别', sort: true},
                {field: 'username', width: "10%", title: '登录账号'},
                {field: 'realname', width: "10%", title: '姓名'},
                {field: 'mob', width: "10%", title: '手机号'},
                //{field: 'cash', title: '签名', minWidth: 150},
                {field: 'cash', width: "10%", title: '余额', sort: true},
                {field: 'status', width: "6%", title: '状态', sort: true, templet: setStatus},
                {title: '操作', width: "17%", toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });
        function setStatus(data) {
            let status = data.status;
            if (status == "正常") {
                return "<font color=green>正常</font>";
            }
            if (status == "冻结") {
                return "<font color=red>冻结</font>";
            }
        }
        //监听复杂搜索
            form.on('submit(data-search-btn-high)', function (data) {
                table.reload('currentTableId', {
                    url: '/api/agent/list',
                    page: {
                        curr: 1
                    },
                    where: {
                        params: data.field
                    }
                });
                //关闭层，使用all解决表单验证生效后
                layer.closeAll();
                $('#popSearchForm')[0].reset();
                $('#popSearch').hide();
                return false;
            });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                url: '/api/agent/list',
                page: {
                    curr: 1
                },
                where: {
                    params: data.field
                }
            });

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/agent/create',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听批量删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length > 0) {
                    layer.confirm('确定删除吗？', function (index) {
                        let idArr = [];
                        for (let item of data) {
                            idArr.push(item.id);
                        }
                        let idStr = idArr.join(',');
                        //layer.alert(JSON.stringify(data));
                        //console.log(obj.data);
                        $.get({
                            dataType: 'text',
                            url: '/api/agent/delete?id=' + idStr,
                            success: function (status) {
                                //删除成功后刷新当前页
                                location.reload();
                            }
                        });
                    });
                }
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            //console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                let index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/agent/edit?id=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除吗？', function (index) {
                    $.get({
                        dataType: 'text',
                        url: '/api/agent/delete?id=' + obj.data.id,
                        success: function (status) {
                            obj.del();
                            //console.log(status);
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'recharge') {
                //充值扣款
                layer.prompt({
                    formType: 3,
                    value: '',
                    title: '充值扣款（<font color=red>扣款使用负数</font>）',
                    btn: ['充值/扣款', '取消']
                }, function(value, index, elem){
                    let rex = /^(\-|\+)?\d+(\.\d+)?$/;
                    if (!rex.test(value)) {
                        layer.msg('只能输入正数、负数，小数超过两位仅取两位');
                        return false;
                    }
                    $(".layui-layer-btn0").remove();
                    $.get({
                        dataType: 'text',
                        url: '/api/agent/recharge?id=' + obj.data.id + '&amount=' + value,
                        success: function (status) {
                            let temp = JSON.parse(status);
                            if (temp.code == 200) {
                                layer.close(index);
                                parent.location.reload();
                            } else if ((temp.code == 1001)) {
                                layer.msg('操作失败，请稍后再试');
                            } else if ((temp.code == 1003)) {
                                layer.msg('余额不足');
                            }
                            $(".layui-layer-btn0").show();
                        },
                        //处理422
                        error:function (err) {
                            if (err.status == 422) {
                                let json = JSON.parse(err.responseText);
                                json = json.errors;
                                let errorInfo = [];
                                for (let item in json) {
                                    for (let i = 0; i < json[item].length; i++) {
                                        errorInfo.push(json[item][i]);
                                    }
                                }
                                let errStr = errorInfo.join('<br>');
                                layer.open({
                                    title: '填写错误'
                                    ,content: errStr
                                });
                                $(".layui-layer-btn0").show();
                            }
                            return false;
                        },

                    });

                });
            }
        });

    });

</script>

</body>
</html>
